﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Novacode;

namespace 电子病历
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.printDocument1.OriginAtMargins = true;//启用页边距
            this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
        }
        string constr = ConfigurationManager.ConnectionStrings["EMR"].ConnectionString;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //this.printPreviewDialog1.ShowDialog();
            panel1.Enabled = true;
        }
        ////打印设置
        //private void btnSetPrint_Click(object sender, EventArgs e)
        //{
        //    this.pageSetupDialog1.ShowDialog();
        //}

        ////打印预览
        //private void btnPrePrint_Click(object sender, EventArgs e)
        //{
        //    this.printPreviewDialog1.ShowDialog();
        //}

        ////打印
        //private void btnPrint_Click(object sender, EventArgs e)
        //{
        //    if (this.printDialog1.ShowDialog() == DialogResult.OK)
        //    {
        //        this.printDocument1.Print();
        //    }
        //}

        //打印内容的设置
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            ////打印内容 为 整个Form
            //Image myFormImage;
            //myFormImage = new Bitmap(this.Width, this.Height);
            //Graphics g = Graphics.FromImage(myFormImage);
            //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
            //e.Graphics.DrawImage(myFormImage, 0, 0);

            ////打印内容 为 局部的 this.groupBox1
            //Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
            //groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);

            //打印内容 为 自定义文本内容 
            //Font font = new Font("宋体", 12);
            //Brush bru = Brushes.Blue;
            //for (int i = 1; i <= 5; i++)
            //{
            //    e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
            //}
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "INSERT INTO [b_zd]([zyh],[hz_id],[hz_cwh] ,[zy_zd1] ,[zy1] ,[hz1]  ,[wy1] ,[sw1] ,[qt1] ,[zy_icd1] ,[zy_zd2] ,[zy2] ,[hz2] ,[wy2]"
                + ",[sw2] ,[qt2] ,[zy_icd2] ,[zy_zd3] ,[zy3],[hz3] ,[wy3] ,[sw3],[qt3] ,[zy_icd3] ,[zy_zd4] ,[zy4] ,[hz4] ,[wy4] ,[sw4]"
                + ",[qt4] ,[zy_icd4]  ,[zy_zd5] ,[zy5] ,[hz5] ,[wy5] ,[sw5] ,[qt5] ,[zy_icd5] ,[zy_zd6] ,[zy6] ,[hz6] ,[wy6] ,[sw6],[qt6]"
                + ",[zy_icd6] ,[zy_zd7] ,[zy7] ,[hz7] ,[wy7] ,[sw7] ,[qt7] ,[zy_icd7] ,[zy_zd8] ,[zy8] ,[hz8] ,[wy8] ,[sw8] ,[qt8] ,[zy_icd8],[zy_zd9] ,[zy9] ,[hz9] ,[wy9],[sw9] ,[qt9] ,[zy_icd9]) VALUES"
                + "(" +"'"+ lbzhuyuanhao.Text +"'"+ "," +"'"+ hz_id +"'"+ "," +"'"+ "床位号" +"'"+ ","
                + "'" + txtzyzd1.Text + "'" + "," + "'" + comzy1.Text + "'" + "," + "'" + comhz1.Text + "'" + "," + "'" + comwy1.Text + "'" + "," + "'" + comsw1.Text + "'" + "," + "'" + comqt1.Text + "'" + "," + "'" + txtzyicd1.Text + "'" + ","
                + "'" + txtzyzd2.Text + "'" + "," + "'" + comzy2.Text + "'" + "," + "'" + comhz2.Text + "'" + "," + "'" + comwy2.Text + "'" + "," + "'" + comsw2.Text + "'" + "," + "'" + comqt2.Text + "'" + "," + "'" + txtzyicd2.Text + "'" + ","
                + "'" + txtzyzd3.Text + "'" + "," + "'" + comzy3.Text + "'" + "," + "'" + comhz3.Text + "'" + "," + "'" + comwy3.Text + "'" + "," + "'" + comsw3.Text + "'" + "," + "'" + comqt3.Text + "'" + "," + "'" + txtzyicd3.Text + "'" + ","
                + "'" + txtzyzd4.Text + "'" + "," + "'" + comzy4.Text + "'" + "," + "'" + comhz4.Text + "'" + "," + "'" + comwy4.Text + "'" + "," + "'" + comsw4.Text + "'" + "," + "'" + comqt4.Text + "'" + "," + "'" + txtzyicd4.Text + "'" + ","
                + "'" + txtzyzd5.Text + "'" + "," + "'" + comzy5.Text + "'" + "," + "'" + comhz5.Text + "'" + "," + "'" + comwy5.Text + "'" + "," + "'" + comsw5.Text + "'" + "," + "'" + comqt5.Text + "'" + "," + "'" + txtzyicd5.Text + "'" + ","
                + "'" + txtzyzd6.Text + "'" + "," + "'" + comzy6.Text + "'" + "," + "'" + comhz6.Text + "'" + "," + "'" + comwy6.Text + "'" + "," + "'" + comsw6.Text + "'" + "," + "'" + comqt6.Text + "'" + "," + "'" + txtzyicd6.Text + "'" + ","
                + "'" + txtzyzd7.Text + "'" + "," + "'" + comzy7.Text + "'" + "," + "'" + comhz7.Text + "'" + "," + "'" + comwy7.Text + "'" + "," + "'" + comsw7.Text + "'" + "," + "'" + comqt7.Text + "'" + "," + "'" + txtzyicd7.Text + "'" + ","
                + "'" + txtzyzd8.Text + "'" + "," + "'" + comzy8.Text + "'" + "," + "'" + comhz8.Text + "'" + "," + "'" + comwy8.Text + "'" + "," + "'" + comsw8.Text + "'" + "," + "'" + comqt8.Text + "'" + "," + "'" + txtzyicd8.Text + "'" + ","
                + "'" + txtzyzd9.Text + "'" + "," + "'" + comzy9.Text + "'" + "," + "'" + comhz9.Text + "'" + "," + "'" + comwy9.Text + "'" + "," + "'" + comsw9.Text + "'" + "," + "'" + comqt9.Text + "'" + "," + "'" + txtzyicd9.Text + "'" +")";
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();
            MessageBox.Show("保存成功！");
            conn.Close();
            

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Form Me = null;
            // printForm1.Print(this,Microsoft.VisualBasic.PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly);
            printForm1.Print();
        }

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(this.Size.Width, this.Size.Height);
            //this 是窗体 
            this.DrawToBitmap(bmp, this.ClientRectangle);
            e.Graphics.DrawImage(bmp, 0, 0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //panel1.Visible = false;
            panel1.Enabled = false;
            layoutcomb();
            jzhzxx();
            zy_zd();
            
        }
        public void zy_zd()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sqlstr = "select * from b_zd where hz_id="+"'"+hz_id+"'";
            SqlCommand comm = new SqlCommand(sqlstr,conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                txtzyzd1.Text = sdr["zy_zd1"].ToString();
                txtzyzd2.Text = sdr["zy_zd2"].ToString();
                txtzyzd3.Text = sdr["zy_zd3"].ToString();
                txtzyzd4.Text = sdr["zy_zd4"].ToString();
                txtzyzd5.Text = sdr["zy_zd5"].ToString();
                txtzyzd6.Text = sdr["zy_zd6"].ToString();
                txtzyzd7.Text = sdr["zy_zd7"].ToString();
                txtzyzd8.Text = sdr["zy_zd8"].ToString();
                txtzyzd9.Text = sdr["zy_zd9"].ToString();
                comzy1.Text = sdr["zy1"].ToString();
                comzy2.Text = sdr["zy2"].ToString();
                comzy3.Text = sdr["zy3"].ToString();
                comzy4.Text = sdr["zy4"].ToString();
                comzy5.Text = sdr["zy5"].ToString();
                comzy6.Text = sdr["zy6"].ToString();
                comzy7.Text = sdr["zy7"].ToString();
                comzy8.Text = sdr["zy8"].ToString();
                comzy9.Text = sdr["zy9"].ToString();
                comhz1.Text = sdr["hz1"].ToString();
                comhz2.Text = sdr["hz2"].ToString();
                comhz3.Text = sdr["hz3"].ToString();
                comhz4.Text = sdr["hz4"].ToString();
                comhz5.Text = sdr["hz5"].ToString();
                comhz6.Text = sdr["hz6"].ToString();
                comhz7.Text = sdr["hz7"].ToString();
                comhz8.Text = sdr["hz8"].ToString();
                comhz9.Text = sdr["hz9"].ToString();
                comwy1.Text = sdr["wy1"].ToString();
                comwy2.Text = sdr["wy2"].ToString();
                comwy3.Text = sdr["wy3"].ToString();
                comwy4.Text = sdr["wy4"].ToString();
                comwy5.Text = sdr["wy5"].ToString();
                comwy6.Text = sdr["wy6"].ToString();
                comwy7.Text = sdr["wy7"].ToString();
                comwy8.Text = sdr["wy8"].ToString();
                comwy9.Text = sdr["wy9"].ToString();
                comsw1.Text = sdr["sw1"].ToString();
                comsw2.Text = sdr["sw2"].ToString();
                comsw3.Text = sdr["sw3"].ToString();
                comsw4.Text = sdr["sw4"].ToString();
                comsw5.Text = sdr["sw5"].ToString();
                comsw6.Text = sdr["sw6"].ToString();
                comsw7.Text = sdr["sw7"].ToString();
                comsw8.Text = sdr["sw8"].ToString();
                comsw9.Text = sdr["sw9"].ToString();
                txtzyicd1.Text = sdr["zy_icd1"].ToString();
                txtzyicd2.Text = sdr["zy_icd2"].ToString();
                txtzyicd3.Text = sdr["zy_icd3"].ToString();
                txtzyicd4.Text = sdr["zy_icd4"].ToString();
                txtzyicd5.Text = sdr["zy_icd5"].ToString();
                txtzyicd6.Text = sdr["zy_icd6"].ToString();
                txtzyicd7.Text = sdr["zy_icd7"].ToString();
                txtzyicd8.Text = sdr["zy_icd8"].ToString();
                txtzyicd9.Text = sdr["zy_icd9"].ToString();
            }
            conn.Close();
        }
        public void layoutcomb()
        {
            lbdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            comzy1.SelectedIndex = 0;
            comzy2.SelectedIndex = 0;
            comzy3.SelectedIndex = 0;
            comzy4.SelectedIndex = 0;
            comzy5.SelectedIndex = 0;
            comzy6.SelectedIndex = 0;
            comzy7.SelectedIndex = 0;
            comzy8.SelectedIndex = 0;
            comzy9.SelectedIndex = 0;
            comhz1.SelectedIndex = 0;
            comhz2.SelectedIndex = 0;
            comhz3.SelectedIndex = 0;
            comhz4.SelectedIndex = 0;
            comhz5.SelectedIndex = 0;
            comhz6.SelectedIndex = 0;
            comhz7.SelectedIndex = 0;
            comhz8.SelectedIndex = 0;
            comhz9.SelectedIndex = 0;
            comwy1.SelectedIndex = 0;
            comwy2.SelectedIndex = 0;
            comwy3.SelectedIndex = 0;
            comwy4.SelectedIndex = 0;
            comwy5.SelectedIndex = 0;
            comwy6.SelectedIndex = 0;
            comwy7.SelectedIndex = 0;
            comwy8.SelectedIndex = 0;
            comwy9.SelectedIndex = 0;
            comsw1.SelectedIndex = 0;
            comsw2.SelectedIndex = 0;
            comsw3.SelectedIndex = 0;
            comsw4.SelectedIndex = 0;
            comsw5.SelectedIndex = 0;
            comsw6.SelectedIndex = 0;
            comsw7.SelectedIndex = 0;
            comsw8.SelectedIndex = 0;
            comsw9.SelectedIndex = 0;
            comqt1.SelectedIndex = 0;
            comqt2.SelectedIndex = 0;
            comqt3.SelectedIndex = 0;
            comqt4.SelectedIndex = 0;
            comqt5.SelectedIndex = 0;
            comqt6.SelectedIndex = 0;
            comqt7.SelectedIndex = 0;
            comqt8.SelectedIndex = 0;
            comqt9.SelectedIndex = 0;
        }
        string hz_id = "";
        public void jzhzxx()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string str1 =string.Format( "select * from h_zyxx inner join h_huanz on h_huanz.id=h_zyxx.hz_id where djh='{0}'",chuanzhi.hz_cwh);
            SqlCommand comm = new SqlCommand(str1, conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                hz_id= sdr["hz_id"].ToString();
                lbzhuyuanhao.Text = sdr["djh"].ToString();
                lbbinganhao.Text = sdr["id"].ToString();
                lbxingming.Text = sdr["xm"].ToString();
                lbyiliaofukuanfangshi.Text = sdr["fylb"].ToString();
                if (sdr["xb"].ToString() == "1")
                {
                    lbxingbie.Text = "男";
                }
                else
                {
                    lbxingbie.Text = "女";
                }
                //lbxingbie.Text = sdr["xb"].ToString();
                lbchushengriqi.Text = sdr["csrq"].ToString();
                lbnianling.Text = sdr["nianl"].ToString();
                lbhunfou.Text = sdr["hf"].ToString();
                lbzhiye.Text = sdr["zy"].ToString();
                lbchushengdi.Text = sdr["csd"].ToString();
                lbminzu.Text = sdr["minz"].ToString();
                lbguoji.Text = sdr["gj"].ToString();
                lbshenfenzheng.Text = sdr["sfzh"].ToString();
                lbdianhua.Text = sdr["dh"].ToString();
                lbyouzhengbianma.Text = sdr["gzyzbm"].ToString();
                lbjiatingdizhi.Text = sdr["dz"].ToString();
                lblianxiren.Text = sdr["lxr"].ToString();
                lblianxirenguanxi.Text = sdr["lxrgx"].ToString();
                lblianxirendianhua.Text = sdr["lxrdh"].ToString();
                lblianxirendizhi.Text = sdr["lxrdz"].ToString();
                lbruyuanriqi.Text = sdr["zyrq"].ToString();
                lbruyuankeshi.Text = sdr["ryks"].ToString();
                lbbingqu.Text = sdr["bq_id"].ToString();
                lbzhuanrukeshi.Text = sdr["zrks"].ToString();
                lbchuyuanriqi.Text = sdr["cyrq"].ToString();
                lbchuyuankeshi.Text = sdr["cyks"].ToString();
                lbchuyuanbingqu.Text = sdr["cybq"].ToString();
                lbzhuyuantianshu.Text = sdr["days"].ToString();
                txtmz_zd.Text = sdr["mzzd"].ToString();
                lbruyuanqingkuang.Text = sdr["ryqk"].ToString();
                lbruyuanzhenduan.Text = sdr["ryzd"].ToString();
                lbryhqzrq.Text = sdr["qzrq"].ToString();


            }
        }

        private void txtzyzd1_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd1.Text = chuanzhi.zyzd;
            txtzyicd1.Text = chuanzhi.zyicd;
        }

        private void txtzyzd2_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd2.Text = chuanzhi.zyzd;
            txtzyicd2.Text = chuanzhi.zyicd;
        }

        private void txtzyzd3_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd3.Text = chuanzhi.zyzd;
            txtzyicd3.Text = chuanzhi.zyicd;
        }

        private void txtzyzd4_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd4.Text = chuanzhi.zyzd;
            txtzyicd4.Text = chuanzhi.zyicd;
        }

        private void txtzyzd5_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd5.Text = chuanzhi.zyzd;
            txtzyicd5.Text = chuanzhi.zyicd;
        }

        private void txtzyzd6_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd6.Text = chuanzhi.zyzd;
            txtzyicd6.Text = chuanzhi.zyicd;
        }

        private void txtzyzd7_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd7.Text = chuanzhi.zyzd;
            txtzyicd7.Text = chuanzhi.zyicd;
        }

        private void txtzyzd8_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd8.Text = chuanzhi.zyzd;
            txtzyicd8.Text = chuanzhi.zyicd;
        }

        private void txtzyzd9_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtzyzd9.Text = chuanzhi.zyzd;
            txtzyicd9.Text = chuanzhi.zyicd;
        }

        private void textBox2_DoubleClick(object sender, EventArgs e)
        {
            chuanzhi.zyzd = "";
            住院诊断选择 zyz = new 住院诊断选择();
            zyz.ShowDialog();
            txtmz_zd.Text = chuanzhi.zyzd;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            using (var document = DocX.Load(Application.StartupPath+@"\病案首页.docx"))
            {
                document.ReplaceText("[姓名] ",lbxingming.Text);
                document.ReplaceText("[性别] ", lbxingbie.Text);
                document.ReplaceText("[出生日期] ", lbchushengriqi.Text);
                document.ReplaceText("[年龄] ", lbnianling.Text);
                document.ReplaceText("[婚否] ", lbhunfou.Text);
                document.ReplaceText("[职业] ", lbzhiye.Text);
                document.ReplaceText("[籍贯] ", lbchushengdi.Text);
                document.ReplaceText("[民族] ", lbminzu.Text);
                document.ReplaceText("[国籍] ", lbguoji.Text);
                document.ReplaceText("[身份证号] ", lbshenfenzheng.Text);
                document.ReplaceText("[工作单位] ", lbgongzuodanwei.Text);
                document.ReplaceText("[家庭电话] ", lbdianhua.Text);

                document.ReplaceText("[邮政编码(单位)] ", lbyouzhengbianma.Text);
                document.ReplaceText("[联系人] ", lblianxiren.Text);
                document.ReplaceText("[关系] ", lblianxirenguanxi.Text);
                document.ReplaceText("[联系电话] ", lblianxirendianhua.Text);
                document.ReplaceText("[住院日期] ", lbruyuanriqi.Text);
                document.ReplaceText("[科室] ", lbruyuankeshi.Text);
                document.ReplaceText("[病区] ", lbbingqu.Text);
                document.ReplaceText("[转入科室] ", lbzhuanrukeshi.Text);
                document.ReplaceText("[出院日期] ", lbchuyuanriqi.Text);
                document.ReplaceText("[出院科室] ", lbchuyuankeshi.Text);
                document.ReplaceText("[住院病区] ", lbchuyuanbingqu.Text);
                document.ReplaceText("[住院天数] ", lbzhuyuantianshu.Text);
                document.ReplaceText("[门诊诊断] ", txtmz_zd.Text);
                document.ReplaceText("[入院情况] ", lbruyuanqingkuang.Text);
                document.ReplaceText("[入院诊断] ", lbruyuanzhenduan.Text);
                document.ReplaceText("[入院后确诊日期]", lbryhqzrq.Text);
                document.ReplaceText("[住院诊断1]", txtzyzd1.Text);
                document.ReplaceText("[住院诊断2]", txtzyzd2.Text);
                document.ReplaceText("[住院诊断3]", txtzyzd3.Text);
                document.ReplaceText("[住院诊断4]", txtzyzd4.Text);
                document.ReplaceText("[住院诊断5]", txtzyzd5.Text);



                document.Save();
            }
        }
    }
}
